package com.itheima.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.entity.Department;
import com.itheima.entity.Employee;
import com.itheima.service.DepartmentService;
import com.itheima.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/emp")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;
    @Autowired
    private DepartmentService departmentService;

    //查询员工信息，跳转系统主页
    @RequestMapping("/list.html")
    public String main(Integer currentPage, ModelMap modelMap) {
        if(currentPage == null) {
            currentPage = 1;
        }
        PageHelper.startPage(currentPage,6);
        //调用业务层查询所有员工
        List<Employee> emps = employeeService.findAll();
        PageInfo<Employee> pageInfo = new PageInfo<>(emps,5); //连续显示5页
        modelMap.addAttribute("pageInfo",pageInfo);
        //获取当前页页码
        int pageNum = pageInfo.getPageNum();
        modelMap.addAttribute("pageNum",pageNum);

        //查询部门信息
        List<Department> depts = departmentService.findAll();
        modelMap.addAttribute("depts",depts);
        return "emp/list";
    }

    //多条件查询
    @RequestMapping("findByCondition")
    public String findByCondition(Integer empId,String name,String sex,Integer deptId,
                                  Integer currentPage, ModelMap modelMap) {
        if(currentPage == null) {
            currentPage = 1;
        }
        PageHelper.startPage(currentPage,6);
        List<Employee> emps = employeeService.findByCondition(empId,name,sex,deptId);
        PageInfo<Employee> pageInfo = new PageInfo<>(emps,5); //连续显示5页
        modelMap.addAttribute("pageInfo",pageInfo);
        //获取当前页页码
        int pageNum = pageInfo.getPageNum();
        modelMap.addAttribute("pageNum",pageNum);

        //查询部门信息
        List<Department> depts = departmentService.findAll();
        modelMap.addAttribute("depts",depts);
        return "emp/list";
    }

    //跳转添加页
    @RequestMapping("/add.html")
    public String add(ModelMap modelMap) {
        //查询部门信息
        List<Department> depts = departmentService.findAll();
        modelMap.addAttribute("depts",depts);
        return "emp/add";
    }

    //添加操作
    @PostMapping("/save")
    @ResponseBody
    public Map<String,Object> saveEmp(Employee employee) {
        Map<String,Object> map = new HashMap<>();
        employeeService.saveEmp(employee);
        map.put("success",true);
        map.put("message","添加成功");
        return map;
    }

    //删除操作
    @RequestMapping("/delete")
    @ResponseBody
    public Map<String,Object> deleteEmp(Integer empId) {
        Map<String,Object> map = new HashMap<>();
        employeeService.deleteEmp(empId);
        map.put("success",true);
        map.put("message","删除成功");
        return map;
    }

    //跳转修改页
    @RequestMapping("/edit.html")
    public String edit(Integer empId,Integer currentPage,ModelMap modelMap) {
        //根据id查询员工，表单回显
        Employee employee = employeeService.findById(empId);
        //查询部门信息
        List<Department> depts = departmentService.findAll();
        modelMap.addAttribute("emp",employee);
        modelMap.addAttribute("depts",depts);
        //再将当前页码存到域中
        modelMap.addAttribute("pageNum",currentPage);
        return "emp/edit";
    }

    //修改操作
    @PostMapping("/update")
    @ResponseBody
    public Map<String,Object> updateEmp(Employee employee) {
        Map<String,Object> map = new HashMap<>();
        employeeService.updateEmp(employee);
        map.put("success",true);
        map.put("message","修改成功");
        return map;
    }
}
